﻿

Public Class Site
    Inherits System.Web.UI.MasterPage
    Implements IMasterPage




    Public Event OnScoutChanged() Implements IMasterPage.OnScoutChanged
    Private ET As New ETModelContainer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            LoadScouts()

        End If

        mnuMain.Items.FindDataMenuItemByText("Administration").Visible = CurrentUser.IsInRole(UNIT_ADMIN, ANY_UNITS, ET)
        mnuMain.Items.FindDataMenuItemByText("My Scouts").Visible = CurrentUser.User.Identity.IsAuthenticated
        mnuMain.Items.FindDataMenuItemByText("Awards").Visible = CurrentUser.User.Identity.IsAuthenticated
        mnuMain.Items.FindDataMenuItemByText("Fundraising").Visible = CurrentUser.User.Identity.IsAuthenticated
    End Sub

    Public ReadOnly Property IsInAsynchPostback() As Boolean Implements IMasterPage.IsInAsynchPostback
        Get
            Return ScriptManager1.IsInAsyncPostBack
        End Get
    End Property


    Public ReadOnly Property CurrentScoutID1 As Integer Implements IMasterPage.CurrentScoutID
        Get
            If cboScout.Items.Count = 0 Then
                LoadScouts()
            End If
            If cboScout.Items.Count = 0 Then
                cboScout.Enabled = False
                Return 0
            End If
            If cboScout.SelectedIndex >= 0 Then

                Return CInt(cboScout.Items(cboScout.SelectedIndex).Value)
            Else
                Return CInt(cboScout.Items(0).Value)
            End If

        End Get
    End Property

    Private Sub LoadScouts()
        Dim data = From x In ET.Scouts Where x.UserID = HttpContext.Current.User.Identity.Name Select x
        cboScout.DataSource = data
        cboScout.DataBind()
    End Sub
    Public ReadOnly Property CurrentScout As Scout Implements IMasterPage.CurrentScout
        Get
            Dim scout As Scout = (From s In ET.Scouts Where s.ScoutID = CurrentScoutID1).SingleOrDefault
            Return scout
        End Get
    End Property


    Private Sub cboScout_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboScout.SelectedIndexChanged
        RaiseEvent OnScoutChanged()
    End Sub




    Public ReadOnly Property CurrentUser As MyUser Implements IMasterPage.CurrentUser
        Get
            Return New MyUser(HttpContext.Current.User)
        End Get
    End Property

    Public ReadOnly Property CurrentUserName As String Implements IMasterPage.CurrentUserName
        Get
            Return CurrentUser.User.Identity.Name
        End Get

    End Property

 
End Class

Public Interface IMasterPage

    ReadOnly Property CurrentScoutID As Integer
    ReadOnly Property IsInAsynchPostback() As Boolean
    ReadOnly Property CurrentScout As Scout
    ReadOnly Property CurrentUser As MyUser

    Event OnScoutChanged()

    ReadOnly Property CurrentUserName As String


End Interface